package com.qf.springbootproject.product.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qf.springbootproject.es.SelectEsVo;
import com.qf.springbootproject.index.entity.Product;
import com.qf.springbootproject.index.entity.ProductImg;
import com.qf.springbootproject.index.service.ProductService;
import com.qf.springbootproject.product.service.ProductCommentsService;
import com.qf.springbootproject.product.entity.ProductParams;
import com.qf.springbootproject.product.service.ProductParamsService;
import com.qf.springbootproject.product.entity.ProductSku;
import com.qf.springbootproject.product.service.ProductSkuService;
import com.qf.springbootproject.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品规格 每一件商品都有不同的规格，不同的规格又有不同的价格和优惠力度，规格表为此设计 前端控制器
 * </p>
 *
 * @author yyq
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/product/")
@CrossOrigin
public class ProductController {
    @Autowired
    ProductSkuService productSkuService;
    @Autowired
    ProductParamsService productParamsService;
    @Autowired
    ProductCommentsService productCommentsService;
    @Autowired
    ProductService productService;

    /**
     * 1.根据商品id查询商品基本信息优化版
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-info/{id}")
    public ResultVo detailInfo(@PathVariable("id") Integer id) {
        Map<String, Object> data = productService.detailInfo(id);
        return ResultVo.ok(data);
    }

    /**
     * 2.根据商品id查询商品生产信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-params/{id}")
    public ResultVo productAcclaim(@PathVariable("id") Integer id) {
        ProductParams productParamsList = productParamsService.productAcclaim(id);
        ResultVo ok = ResultVo.ok();
        ok.put("data", productParamsList);
        return ok;
    }

    /**
     * 3.分页查询商品评价
     *
     * @param id
     * @param pageNum
     * @param limit
     * @return
     */
    @RequestMapping("/detail-commonts/{id}")
    public ResultVo productComments(@PathVariable("id") Integer id, Integer pageNum, Integer limit) {
        ResultVo resultVo = productCommentsService.productComments(id, pageNum, limit);
        return resultVo;
    }

    /**
     * /4.根据商品id查询商品好评率
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-commontscount/{id}")
    public ResultVo rating(@PathVariable("id") Integer id) {
        ResultVo resultVo = productCommentsService.rating(id);
        return resultVo;
    }

    /**
     * 添加商品
     *
     * @param productParams
     * @return
     */
    @PostMapping("/insert")
    public ResultVo insertProduct(@RequestBody ProductParams productParams) throws JsonProcessingException {
        //productParamsService.insertProduct(productParams);
        productParamsService.sendProductEs(productParams);
        return ResultVo.ok();
    }

    @PostMapping("/boolQuery")
    public ResultVo boolQuery(@RequestBody SelectEsVo selectEsVo) throws JsonProcessingException {
       List<Map<String,Object>> list = productParamsService.boolQuery(selectEsVo);
       return ResultVo.ok(list);
    }

}
